//
//  ZPStatusToolBarView.swift
//  ZPWeibo
//
//  Created by zhangpeng on 16/4/21.
//  Copyright © 2016年 zhangpeng. All rights reserved.
//

import UIKit
import SnapKit
class ZPStatusToolBarView: UIView {
    //转发
    var retweetButton:UIButton?
    //评论
    var commentButton:UIButton?
    //赞
    var unlikeButton:UIButton?
    
    var status:ZPStatus?{
    
        didSet{
        
        let retweetResult = getCountString(status?.reposts_count ?? 0, title: "转发")
            
        let commentResult = getCountString(status?.comments_count ?? 0, title: "评论")
            
        let unlikeResult = getCountString(status?.attitudes_count ?? 0, title: "赞")
           
        retweetButton?.setTitle(retweetResult, forState: UIControlState.Normal)
        
        unlikeButton?.setTitle(commentResult, forState: UIControlState.Normal)
            
        unlikeButton?.setTitle(unlikeResult, forState: UIControlState.Normal)
            
            let result = getCountString(10000, title: "测试")
            print(result)
        }
    }
    
    /*
    
    - 底部tool 显示 转发 评论 赞 的业务逻辑
    - 如果我们的 count <= 0  直接显示  转发 评论 赞 文字
    -例如 count == 0  显示 转发 评论 赞
    - 如果我们的 count > 0 && count < 10000  直接显示 count
    -例如 count == 888  显示 888
    - 如果我们的 count >= 10000  显示 1万 2万
    -例如 count 10000  显示 1万   20000  2万
    -例如 count 12000  显示 1.2万
    
    */
    
    func getCountString(count: Int , title:String) -> String{
    
        if count <= 0{
        
            return title
        }else if count > 0 && count < 10000{
        
            return "\(count)"
        }else{
            // 转成 CGFloat
           let num = (count / 1000) / 10
            // Int 转字符串
            var countString = "\(num)万"
            
            // 判断我们的字符串中 是否有'.0'
            if countString.containsString(".0"){
            
                countString = countString.stringByReplacingOccurrencesOfString(".0万", withString: "万")
                
            }
            return countString
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }
    
    private func setupUI(){
    
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        //添加控件
        retweetButton = addChildButton("timeline_icon_retweet", title: "转发")
        
        commentButton = addChildButton("timeline_icon_comment", title: "评论")
        
         unlikeButton =  addChildButton("timeline_icon_unlike", title: "赞")
        //添加两根竖线
        let line1 = addChildLineImageView()
        let line2 = addChildLineImageView()
        
        //添加约束
        
        retweetButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.leading.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        
        
        commentButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.width.equalTo(unlikeButton!)
        }
        
        
        unlikeButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        
        line1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(retweetButton!)
        }
        
        
        line2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(commentButton!)
        }
        
    }
    // 创建button
    func addChildButton(imgName:String , title:String) -> UIButton{
    
         // 初始化button
        let button = UIButton()
        
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        
        addSubview(button)
        
        return button
        
    }
     // 创建竖线
    func addChildLineImageView() ->UIImageView{
    
        let img = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        
        addSubview(img)
        
        return img
    }
    
}
